package com.example.seakdreamandroid.whiteNoise;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.example.seakdreamandroid.Constant;
import com.example.seakdreamandroid.MainActivity;
import com.example.seakdreamandroid.R;
import com.example.seakdreamandroid.entity.AudioInfo;
import com.example.seakdreamandroid.whiteNoisePlayer.WhiteNoisePlayerActivity;
import com.example.seakdreamandroid.whiteNoisePlayer.WhiteNoisePlayerService;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/*
杨锐涵 白噪音官方推荐界面
 */

public class OfficialRecommendation extends Fragment {
    private static Context mContext;
    private static List<WhiteNoiseOfficialChannel> channels;
    private static WhiteNoiseOfficialChannelAdapter channelAdapter;
    public OfficialRecommendation(){}
    public OfficialRecommendation(Context mContext) {
        this.mContext = mContext;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        //获取布局文件
        View view = inflater.inflate(R.layout.layout_whitenoise_fragment_listview,container,false);
        //获取布局文件中的控件对象
        ListView listView = view.findViewById(R.id.white_noise_fragment_listview);
        //给控件对象设置必要的属性
        //给某些控件对象添加事件监听器

        //获取Channel数据源
        channels = setDate();
        //实例化适配器对象
        channelAdapter = new WhiteNoiseOfficialChannelAdapter(
                mContext,
                channels
        );
        init();
        listView.setAdapter(channelAdapter);
        listView.setOnItemClickListener(
                new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        //日志显示被点击的信息
                        //首先获取点击对象
                        WhiteNoiseOfficialChannel c = channels.get(position);
                        //其次日志显示获取到的对象
                        Log.i(OfficialRecommendation.class.getName(),"选中：" + c);
                        startToTarActivity(position);

                    }
                }
        );
        return view;
    }

    private void init() {
    }
    public Handler handler = new Handler(Looper.getMainLooper()){
        @Override
        public void handleMessage(@NonNull Message msg) {
            switch (msg.what){
                case 4:
                    //更新adapter
                    channelAdapter.notifyDataSetChanged();
                    break;
            }
        }
    };
    private void startToTarActivity(int position) {

        WhiteNoiseOfficialChannel channel = channels.get(position);
        String URL = Constant.BASE_URL +  "WhiteNoise/FreeConbination/findFreeConbinationById/" + channel.getSelectedId1() + "/" + channel.getSelectedId2() + "/" + channel.getSelectedId3();
        OkHttpClient okHttpClient = new OkHttpClient();
        Request request = new Request.Builder()
                .url(URL)

                .build();
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e) {
                Log.e("onFailure","请求失败");
                e.printStackTrace();
            }

            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                String responseBody = response.body().string();
                Gson gson = new Gson();
                Type type= new TypeToken<List<AudioInfo>>(){}.getType();
                List<AudioInfo> mlist = gson.fromJson(responseBody,type);
                WhiteNoisePlayerService.getController().play(mlist,"OfficialRecommendation",null,true);
            }
        });

        //创建Intent对象
        Intent intent = new Intent(mContext, WhiteNoisePlayerActivity.class);
        //跳转
        startActivity(intent);
    }

    private List<WhiteNoiseOfficialChannel> setDate() {
        List<WhiteNoiseOfficialChannel> mlist = new ArrayList<>();

        String URL = Constant.BASE_URL + "WhiteNoise/OfficialRecommendation/findAllOfficialRecommendation";
        OkHttpClient okHttpClient = new OkHttpClient();
        Request request = new Request.Builder()
                .url(URL)
                .build();
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e) {
                Log.e("onFailure","请求失败");
                e.printStackTrace();
            }

            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                String responsseBody = response.body().string();
                Log.e("responsseBody",responsseBody);
                Gson gson = new Gson();
                Type type = new TypeToken<List<WhiteNoiseOfficialChannel>>(){}.getType();
                List<WhiteNoiseOfficialChannel> whiteNoiseOfficialChannels = gson.fromJson(responsseBody,type);
                Message message = handler.obtainMessage();
                message.what = 4;
                mlist.addAll(0,whiteNoiseOfficialChannels);
                Log.e("channels",mlist.size()+"");
                handler.sendMessage(message);
            }
        });
        return  mlist;
    }

}
